//
//  VSaleAlertController.m
//  STL_ Animation
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "VSaleAlertController.h"
#import "STL.h"
#import "VSalePastListModel.h"
@interface VSaleAlertController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *dateArr;
@property (nonatomic, strong) NSMutableArray *selectedDateArr;
@property (nonatomic, strong) NSString *dateCode;

@end

@implementation VSaleAlertController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.yearArr = [NSMutableArray arrayWithCapacity:0];
    self.dateArr = [NSMutableArray arrayWithCapacity:0];
    self.selectedDateArr = [NSMutableArray arrayWithCapacity:0];
    
    self.pickerView = [[UIPickerView alloc] init];
//    self.pickerView.backgroundColor = [UIColor redColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.center = CGPointMake(ScreenWidth / 2 - 8, self.pickerView.center.y);
    self.dateCode = @"0";
    [self.view addSubview:self.pickerView];
    
    UIAlertAction *alertSureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.delegate requestWithDate:self.dateCode];
        
    }];
    UIAlertAction *alertCancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [self addAction:alertSureAction];
    [self addAction:alertCancelAction];
    
    [self requestDateList];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.yearArr.count;
    } else {
        return self.selectedDateArr.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return self.pickerView.bounds.size.width / 3;
    }
    return self.pickerView.bounds.size.width * 2 / 3;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *pickerViewLabel = [[UILabel alloc] init];
    
    pickerViewLabel.textAlignment = NSTextAlignmentCenter;
    pickerViewLabel.font = [UIFont systemFontOfSize:14];
    pickerViewLabel.backgroundColor = [UIColor clearColor];
    if (component == 0) {
        pickerViewLabel.text = self.yearArr[row];
    } else {
        VSalePastListModel *pastListModel = self.selectedDateArr[row];
        pickerViewLabel.text = [NSString stringWithFormat:@"第%@期(%@ - %@)", pastListModel.no, pastListModel.beginDateText, pastListModel.endDateText];
    }
    return pickerViewLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        NSNumber *year = self.yearArr[row];
        [self.selectedDateArr removeAllObjects];
        for (VSalePastListModel *model in self.dateArr) {
            if (model.year.intValue == year.intValue) {
                [self.selectedDateArr addObject:model];
            }
        }
        [self.pickerView reloadComponent:1];
    }
        VSalePastListModel *model = self.selectedDateArr[row];
        self.dateCode = [NSString stringWithFormat:@"%@", model.dateCode];
    
}
#pragma mark - requestDateList
- (void)requestDateList {
    [SJAFNetTool GEtNetWithUrl:@"http://mapi.yinyuetai.com/vchart/period.json?D-A=0" WithBody:nil headerFile:Dictionary response:SJJSON success:^(id result) {
        for (NSNumber *year in result[@"years"]) {
            [self.yearArr addObject:[NSString stringWithFormat:@"%@", year]];
        }
        for (NSDictionary *dic in result[@"periods"]) {
            VSalePastListModel *pastListModel = [[VSalePastListModel alloc] initWithDataSource:dic];
            [self.dateArr addObject:pastListModel];
            NSNumber *latestYear = self.yearArr[0];
            if (pastListModel.year.intValue == latestYear.intValue) {
                [self.selectedDateArr addObject:pastListModel];
            }
        }
        VSalePastListModel *pastListModel = [self.selectedDateArr firstObject];
        self.dateCode = [NSString stringWithFormat:@"%@", pastListModel.dateCode];
        NSLog(@"%@", self.yearArr);
        NSLog(@"%@", self.selectedDateArr);
        [self.pickerView reloadAllComponents];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
